<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: emails.php 45 2013-09-21 11:56:22Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 45 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:56:22 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');
$languages = get_languages();

switch($_GET['action']) {
	case 'save_mail':
		if(!empty($_POST['email_content'])) {
			$sql_array = array(	'email_address' => $_POST['email_address'],
								'email_address_name' => $_POST['email_address_name'],
								'email_replay_address' => $_POST['email_replay_address'],
								'email_replay_address_name' => $_POST['email_replay_address_name'],
								'email_subject' => $_POST['email_subject'],
								'email_forward' => $_POST['email_forward'],
								(($_POST['type'] =='html')?'email_content_html':'email_content_text') => $_POST['email_content']);
	
			$db->db_perform(TABLE_EMAILS, $sql_array, 'update', 'email_name = '.$db->db_prepare($_POST['mail']).' AND languages_id = '.$db->db_prepare($_POST['lang']));
			$message_stack->add_session(SAVE_MAIL, 'success');
			deleteCacheFolders();
			redirect(href_link('emails.php'));
	
		} else {
			$message_stack->add_session(ERROR_MAIL_CONTENT, 'error', 'true');
			redirect(href_link('emails.php'));
		}
		break;

	case 'save_new_mail':
		$sql_array = array(	'email_name' => strtolower($_POST['mail']),
							'languages_id' => '1',
							'email_address' => $_POST['email_address'],
							'email_address_name' => $_POST['email_address_name'],
							'email_replay_address' => $_POST['email_replay_address'],
							'email_replay_address_name' => $_POST['email_replay_address_name'],
							'email_subject' => $_POST['email_subject'],
							'email_forward' => $_POST['email_forward'],
							'email_content_html' => $_POST['email_html_content'],
							'email_content_text' => $_POST['email_text_content'],
							'email_backup_html' => $_POST['email_html_content'],
							'email_backup_text' => $_POST['email_text_content'],
							'email_timestamp' => time());

		$db->db_perform('emails', $sql_array);
		$message_stack->add_session(SAVE_NEW_MAIL, 'success');
		redirect(href_link('emails.php'));
		break;
		
	case 'reset_mail':
		$backup = $db->db_query("SELECT email_backup_html, email_backup_text FROM ".TABLE_EMAILS." WHERE email_name = '".$_GET['mail']."' AND languages_id = '".(int)$_GET['lang']."' ");
		$restore = $db->db_query("UPDATE 
										".TABLE_EMAILS." 
									SET 
										email_content_html = '".$backup->fields['email_backup_html']."', 
										email_content_text = '".$backup->fields['email_backup_text']."' 
									WHERE 
										email_name = '".$_GET['mail']."' 
									AND 
										languages_id = '".(int)$_GET['lang']."' ");
		$message_stack->add_session(RESTORE_BACKUP, 'success');
		redirect(href_link('emails.php'));
		break;
}

switch($_GET['do']) {
	case 'edit':
		include(DIR_WS_MODULES.'emails/edit.php'); ?>
		<script src="includes/javascript/email_editor/lib/codemirror.js" type="text/javascript"></script>
		<script src="includes/javascript/email_editor/mode/xml/xml.js"></script>
		<script src="includes/javascript/email_editor/mode/javascript/javascript.js"></script>
		<script src="includes/javascript/email_editor/mode/css/css.js"></script>
		<script src="includes/javascript/email_editor/mode/htmlmixed/htmlmixed.js"></script>
		<script type="text/javascript">var editor = CodeMirror.fromTextArea(document.getElementById("email_content"), {mode: "text/html", tabMode: "indent",lineWrapping:true,lineNumbers:true});</script>
		<?php
		break;

	case 'new':
		include(DIR_WS_MODULES.'emails/new.php');
		break;

	default:
		require(DIR_WS_INCLUDES.'metatag.php');
		?>
		<link rel="stylesheet" href="includes/javascript/email_editor/lib/codemirror.css">
		</head>
		<body>
		<?php require(DIR_WS_INCLUDES.'header.php'); ?>
		<div id="wrapper">
			<table class="outerTable" cellspacing="0" cellpadding="0">
				<tr>
				    <td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
						<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
					</td>
				    <td class="boxCenter" valign="top">
						<table border="0" width="100%" cellspacing="0" cellpadding="2">
							<tr>
								<td>
									<table class="table_pageHeading" width="100%" cellspacing="0" cellpadding="0">
										<tr>
											<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td align="right">
									<button class="button" onclick="dialogBox('?do=new', 850, 420, 'Neue Email Vorlage','true')">
										<?php echo NEW_MAIL ?>
									</button>
							</tr>
							<tr>
								<td>
									<table border="0" width="100%" cellspacing="0" cellpadding="2" class="dataTable">
			          					<tr class="dataTableHeadingRow">
			            					<td class="dataTableHeadingContent" width="20%">Email</td>
											<td class="dataTableHeadingContent" colspan="<?php echo sizeof($languages) * 4; ?>"><?php echo ACTION ?></td>
			          					</tr>
										<?php
		
										$mail = $db->db_query("SELECT email_name FROM ".TABLE_EMAILS." WHERE languages_id = '".(int)$_SESSION['languages_id']."' ");
		
										while(!$mail->EOF) {
											$rows++;
											if ($rows % 2 == 0)
									    		$f = 'dataTableRow';
									    	else
									    		$f = '';
											$td .= '<tr class="'.$f.'" onmouseover="this.className=\'dataTableRowOver\';" onmouseout="this.className=\''.$f.'\'">'."\n";
												$td .= '<td>'.constant(strtoupper($mail->fields['email_name'].'_TITLE')).'</td>';
												$td .= '<td>';
												for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
													$td .= '<table align="left" '.($i > 0 ? 'style="border-left: 2px solid #ccc"' : '').'><tr>';
													$td .= '<td style="border: 0">
															<img src="../lang/'.$languages[$i]['directory'].'/'.$languages[$i]['image'].'" alt="'.$languages[$i]['name'].'" />
														</td>';
													$td .= '<td style="border: 0">
															<button class="button" onclick="dialogBox(\'?do=edit&mail='.$mail->fields['email_name'].'&lang='.$languages[$i]['id'].'&type=html\', 850, 420, \''.EDIT_HTML_TITLE.'\', \'true\', \'true\')">
																HTML
															</button>
														</td>';
													$td .= '<td style="border: 0">
															<button class="button" onclick="dialogBox(\'?do=edit&mail='.$mail->fields['email_name'].'&lang='.$languages[$i]['id'].'&type=text\', 850, 420, \''.EDIT_TEXT_TITLE.'\', \'true\', \'true\')">
																Text
															</button>
														</td>';
													$td .= '<td style="border: 0">
															<a href="'.href_link('emails.php','action=reset_mail&mail='.$mail->fields['email_name'].'&lang='.$languages[$i]['id']).'">
																'.image('images/icons/icon_database_go.gif','Originale Mail wiederherstellen').'
															</a>
														</td>';
													$td .= '</tr></table>&nbsp;';
												}
											$td .= '</td></tr>';
										$mail->MoveNext();
										}
										echo $td;
									?>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</div>
		<script type="text/javascript">
			function dialogBox(url, w, h, title, save) {
				<?php if(isset($_SESSION['customer_id'])) { ?>
					if(save == 'true') {
						var extra_button = {"<?php echo BUTTON_SAVE ?>":function(){$("#dialog_form").submit();},"<?php echo BUTTON_CANCEL ?>": function() {$(this).dialog("close");}};
					} else {var extra_button = '';}
					
		            var dialog = $('<div style="display:none" class="loading"></div>').appendTo('body');
		            
		            dialog.dialog({
		                close: function(event, ui) {dialog.remove();},
		                modal: true,
		                title: title,
		                buttons: extra_button,
		                width: w,
		                height: h
		            });
		            dialog.load(url,{},function (responseText, textStatus, XMLHttpRequest){
		            	dialog.removeClass('loading');
		            	$('.ui-dialog-content').css({backgroundImage:'none'});
		            });
		            $('.ui-dialog').css({position:"fixed"});

				<?php } else { ?>
					window.location.href='<?php echo catalog_href_link('login.php', '', 'SSL'); ?>';
				<?php } ?>
				return false;
			}
		</script>
		<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
		</body>
		</html>
		<?php require(DIR_WS_INCLUDES.'application_bottom.php');
		break;
}